package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.AppointmentService;
import cn.wolfcode.car.business.service.StatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author WaveHi
 * @Date 14/11/2021 上午 11:08
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@Service
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper, Appointment> implements AppointmentService {

//    @Autowired
//    private AppointmentMapper appointmentMapper;
//
//    @Autowired
//    private StatementMapper statementMapper;

    @Autowired
    private StatementService statementService;

    @Autowired
    private IUserService userService;

    @Override
    public Appointment getAppointment(Long id) {
        QueryWrapper<Appointment> appointmentQueryWrapper = new QueryWrapper<>();
        appointmentQueryWrapper.eq("id", id);
        Appointment one = getOne(appointmentQueryWrapper);
        return one;
//        return appointmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean update(Appointment appointment) {
        boolean b = updateById(appointment);
        return b;
//        int i = appointmentMapper.updateByPrimaryKey(appointment);
//        return i > 0;
    }

    @Override
    public Boolean add(Appointment appointment) {
        Date date = new Date();
        appointment.setCreateTime(date);
        boolean save = save(appointment);
        return save;
//        int insert = appointmentMapper.insert(appointment);
//        return insert > 0;
    }

    @Override
    public TablePageInfo<Appointment> getPartOfAppointments(AppointmentQuery appointmentQuery) {
        QueryWrapper<Appointment> appointmentQueryWrapper = new QueryWrapper<>();
        appointmentQueryWrapper.like(appointmentQuery.getCustomerName() != null,
                "name", appointmentQuery.getCustomerName())
                .like(appointmentQuery.getCustomerPhone() != null,
                        "phone", appointmentQuery.getCustomerPhone())
                .eq(appointmentQuery.getStatus() != null, "status",
                        appointmentQuery.getStatus());
        return new TablePageInfo<>(list(appointmentQueryWrapper));
//        return new TablePageInfo<>(appointmentMapper.selectByCondition(appointmentQuery));
    }

    @Override
    public TablePageInfo<Appointment> getPartOfAppointments(Integer pageNo, Integer pageSize, AppointmentQuery appointmentQuery) {
        QueryWrapper<Appointment> appointmentQueryWrapper = new QueryWrapper<>();
        appointmentQueryWrapper.like(appointmentQuery.getCustomerName() != null,
                "name", appointmentQuery.getCustomerName())
                .like(appointmentQuery.getCustomerPhone() != null,
                        "phone", appointmentQuery.getCustomerPhone())
                .eq(appointmentQuery.getStatus() != null, "status",
                        appointmentQuery.getStatus());
        return new TablePageInfo<>(list(appointmentQueryWrapper));
//        PageHelper.startPage(pageNo, pageSize);
//        return new TablePageInfo<>(appointmentMapper.selectByCondition(appointmentQuery));
    }

    @Override
    public Boolean cancel(Long id) {
//        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        Appointment appointment = getById(id);
        appointment.setStatus(Appointment.STATUS_CANCEL);
        boolean b = updateById(appointment);
        return b;
//        int i = appointmentMapper.updateByPrimaryKey(appointment);
//        return i > 0;
    }

    @Override
    public Boolean delete(Long id) {
//        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        Appointment appointment = getById(id);
        appointment.setStatus(Appointment.STATUS_DELETE);
        boolean b = updateById(appointment);
        return b;
//        int i = appointmentMapper.updateByPrimaryKey(appointment);
//        return i > 0;
    }

    @Override
    @Transactional
    public Boolean deleteBatch(Long[] ids) {
        Boolean result = false;
        for (Long id : ids) {
            Appointment byId = getById(id);
            byId.setStatus(Appointment.STATUS_DELETE);
            result = updateById(byId) && result;
        }
        return result;
//        int count = 0;
//        for (Long id : ids) {
//            Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
//            appointment.setStatus(Appointment.STATUS_DELETE);
//            count += appointmentMapper.updateByPrimaryKey(appointment);
//        }
//        return count > 0;
    }

    @Override
    public Boolean arrival(Long id) {
        Appointment byId = getById(id);
        byId.setActualArrivalTime(new Date());
        byId.setStatus(Appointment.STATUS_ARRIVAL);
        return updateById(byId);
//        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
//        appointment.setActualArrivalTime(new Date());
//        appointment.setStatus(Appointment.STATUS_ARRIVAL);
//        int i = appointmentMapper.updateByPrimaryKey(appointment);
//        return i > 0;
    }

    @Override
    public Long generateStatement(Long appointmentId) {
//        Statement statement =
//                statementMapper.selectByAppointmentId(appointmentId);
        QueryWrapper<Statement> statementQueryWrapper = new QueryWrapper<>();
        statementQueryWrapper.eq("appointment_id", appointmentId);
        Statement statement = statementService.getOne(statementQueryWrapper);
        statement.setPayee(userService.get(statement.getPayeeId()));
        if(statement == null) {
//            Appointment appointment = appointmentMapper.selectByPrimaryKey(appointmentId);
            Appointment appointment = getById(appointmentId);
            statement = new Statement();
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setDiscountAmount(new BigDecimal("0"));
            statement.setTotalQuantity(new BigDecimal("0"));
            statement.setTotalAmount(new BigDecimal("0"));
            statement.setStatus(Statement.STATUS_CONSUME);
            statement.setCreateTime(new Date());
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setLicensePlate(appointment.getLicensePlate());
            statement.setCarSeries(appointment.getCarSeries());
            statement.setServiceType(Long.valueOf(appointment.getServiceType().toString()));
            statement.setAppointmentId(appointmentId);
            statement.setInfo(appointment.getInfo());
//            statementMapper.insert(statement);
            statementService.save(statement);
        }
        return statement.getId();
    }
}
